
package com.flowmodule.servlet;
import com.companymodule.dao.TbCompanyDAO;
import com.flowmodule.bean.TbFlowCompanyMiddle;
import com.flowmodule.dao.*;
import com.usersmodule.bean.TbUsers;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import util.*;

public class TbFlowCompanyMiddleServlet extends HttpServlet{
    public void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method")==null?"":request.getParameter("method");
        int userId=((TbUsers) request.getSession().getAttribute("Users")).getUserId();
        if("add".equals(method)){
        int companyId=Integer.parseInt(request.getParameter("companyId"));
        int flowId=Integer.parseInt(request.getParameter("flowId"));
            if(new TbFlowCompanyMiddleDAO().add(new TbFlowCompanyMiddle(companyId,flowId,userId))){
            request.getSession().setAttribute("result", "添加成功");
            }else{            
            request.getSession().setAttribute("result", "添加失败");
            }
            goList(request, response);
        }
        if("del".equals(method)){
            if(UtilDAO.delin("Tb_FlowCompanyMiddle", "FlowCompanyMiddleId", request.getParameter("delIds"))){            
            request.getSession().setAttribute("result", "删除成功");
            }else{            
            request.getSession().setAttribute("result", "删除失败");
            }
            goList(request, response);
        }
        if("edit".equals(method)){
        int flowCompanyMiddleId=Integer.parseInt(request.getParameter("flowCompanyMiddleId"));
        int companyId=Integer.parseInt(request.getParameter("companyId"));
        int flowId=Integer.parseInt(request.getParameter("flowId"));
            if(new TbFlowCompanyMiddleDAO().edit(new TbFlowCompanyMiddle(flowCompanyMiddleId,companyId,flowId,userId))){
            request.getSession().setAttribute("result", "修改成功");
            }else{            
            request.getSession().setAttribute("result", "修改失败");
            }
            goList(request, response);
        }
        if ("list".equals(method))goList(request, response);
        if ("listById".equals(method)) {
        	request.getSession(true).setAttribute("tbFlowList",new TbFlowDAO().getTbFlowAll());
        	request.getSession(true).setAttribute("tbCompanyList",new TbCompanyDAO().getTbCompanyAll());
            request.getSession(true).setAttribute("tbFlowCompanyMiddle",new TbFlowCompanyMiddleDAO().getTbFlowCompanyMiddleByFlowCompanyMiddleId(Integer.parseInt(request.getParameter("flowCompanyMiddleId"))));
            response.sendRedirect("FlowModule/TbFlowCompanyMiddleOperate.jsp");
    	}
        if("showadd".equals(method)){
        	request.getSession(true).setAttribute("tbFlowList",new TbFlowDAO().getTbFlowAll());
        	request.getSession(true).setAttribute("tbCompanyList",new TbCompanyDAO().getTbCompanyAll());
            request.getSession(true).removeAttribute("tbFlowCompanyMiddle");
            response.sendRedirect("FlowModule/TbFlowCompanyMiddleOperate.jsp");
        }
    }
    public void goList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	request.getSession(true).setAttribute("tbFlowCompanyMiddleList",new TbFlowCompanyMiddleDAO().getTbFlowCompanyMiddleAll());
        response.sendRedirect("FlowModule/TbFlowCompanyMiddleList.jsp");
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }
}
